package com.koo.modules.kb.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.koo.common.response.R;
import com.koo.common.utils.PageUtils;
import com.koo.common.utils.excel.EasyExcelUtil;
import com.koo.modules.kb.entity.KbUploadMemberDataEntity;
import com.koo.modules.kb.service.KbUploadMemberDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 会员信息上传
 *
 * @author charlin
 * @email 136137465@qq.com
 * @date 2023-01-05 10:11:52
 */
@Slf4j
@Api(tags = "kb会员信息上传")
@RestController
@RequestMapping("third/kb/uploadmemberdata")
public class KbUploadMemberDataController {

    @Autowired
    private KbUploadMemberDataService kbUploadMemberDataService;

    @ApiOperation("获取列表")
    @RequestMapping("/list")
    @RequiresPermissions("kb:kbuploadmemberdata:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = kbUploadMemberDataService.queryPage(params);
        return R.ok().put("page", page);
    }


    @ApiOperation("根据主键查询信息")
    @RequestMapping("/info/{memberId}")
    @RequiresPermissions("kb:kbuploadmemberdata:info")
    public R info(@PathVariable("memberId") String memberId){
		KbUploadMemberDataEntity kbUploadMemberData = kbUploadMemberDataService.getById(memberId);
        return R.ok().setData(kbUploadMemberData);
    }

    @ApiOperation("保存")
    @RequestMapping("/save")
    @RequiresPermissions("kb:kbuploadmemberdata:save")
    public R save(@RequestBody KbUploadMemberDataEntity kbUploadMemberData){
		kbUploadMemberDataService.save(kbUploadMemberData);
        return R.ok();
    }

    @ApiOperation("修改")
    @RequestMapping("/update")
    @RequiresPermissions("kb:kbuploadmemberdata:update")
    public R update(@RequestBody KbUploadMemberDataEntity kbUploadMemberData){
		kbUploadMemberDataService.updateById(kbUploadMemberData);
        return R.ok();
    }

    @ApiOperation("删除")
    @RequestMapping("/delete")
    @RequiresPermissions("kb:kbuploadmemberdata:delete")
    public R delete(@RequestBody String[] memberIds){
		kbUploadMemberDataService.removeByIds(Arrays.asList(memberIds));
        return R.ok();
    }


    @ApiOperation("导入")
    @RequestMapping("/importData")
    @RequiresPermissions("kb:kbuploadmemberdata:import")
    @Transactional(rollbackFor = {Exception.class})
    public R importData(@RequestParam("file") MultipartFile file) {
        try {
            // 读取文件流
            EasyExcel.read(
                    file.getInputStream(),
                    KbUploadMemberDataEntity.class,
                    new EasyExcelUtil(kbUploadMemberDataService))// 监听器
                    .excelType(ExcelTypeEnum.XLSX)
                    .sheet("库存模板")
                    .doRead();
        } catch (IOException e) {
            return R.error("上传失败!");
        }
        return R.ok("上传成功!");
    }

    @ApiOperation("导出")
    @GetMapping("/exportData")
    @RequiresPermissions("kb:kbuploadmemberdata:export")
    public void exportData(HttpServletResponse response) {
        try {
            HorizontalCellStyleStrategy horizontalCellStyleStrategy = EasyExcelUtil.initExportParams( "kb会员信息上传报表", response);
            List<KbUploadMemberDataEntity> list = kbUploadMemberDataService.list(null);
            EasyExcel.write(response.getOutputStream()
                    , KbUploadMemberDataEntity.class)
                    .registerWriteHandler(horizontalCellStyleStrategy) //加载样式
                    .sheet()
                    .doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
